﻿using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Data;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.VisualBasic;
using System.IO;

namespace ConsoleApp2
{
    public class LeaveEntityConfig : IEntityTypeConfiguration<Leave>
    {
        public void Configure(EntityTypeBuilder<Leave> builder)
        {
            builder.ToTable("T_Leaves");
            builder.HasOne(l => l.Requester).WithMany()
                .OnDelete(DeleteBehavior.NoAction) // 删除时，级联关系，无动作
                .IsRequired();
            builder.HasOne(l => l.Approver).WithMany()
                // 删除时，级联关系，默认，onDelete: ReferentialAction.Cascade
                ;

            //Failed executing DbCommand(37ms)[Parameters = [], CommandType = 'Text', CommandTimeout = '30']
            //CREATE TABLE[T_Leaves] (
            //    [Id] bigint NOT NULL IDENTITY,
            //    [RequesterId] bigint NOT NULL,
            //    [ApproverId] bigint NOT NULL,
            //    [Remarks] nvarchar(max) NOT NULL,
            //    CONSTRAINT[PK_T_Leaves] PRIMARY KEY([Id]),
            //    CONSTRAINT[FK_T_Leaves_T_Users_ApproverId] FOREIGN KEY([ApproverId]) REFERENCES[T_Users]([Id]) ON DELETE CASCADE,
            //    CONSTRAINT[FK_T_Leaves_T_Users_RequesterId] FOREIGN KEY([RequesterId]) REFERENCES[T_Users]([Id]) ON DELETE CASCADE
            //);
            //Microsoft.Data.SqlClient.SqlException(0x80131904): Introducing FOREIGN KEY constraint 'FK_T_Leaves_T_Users_RequesterId'
            //on table 'T_Leaves' may cause cycles or multiple cascade paths.Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or
            //modify other FOREIGN KEY constraints.
            //Could not create constraint or index.See previous errors.
            //builder.Property(c => c.Message).IsRequired().IsUnicode();
        }
    }
}
